<?php
namespace app\common\upload;

use app\auth\common\util\ApiUtil;
use think\exception\ValidateException;

class Image
{
    public function upload(){
        $file = request()->file('photo');
        try{
            validate(['file' => [
                'fileSize' => 1048576,
                'fileExt' => 'jpg,png,gif,jpeg',
                'fileMime' => 'image/jpeg,image/png,image/gif',
            ]])->check(['file' => $file]);
            $savename = \think\facade\Filesystem::disk('public')->putFile( 'images', $file);
            if(!$savename) ApiUtil::abort('上传失败');
            $filename = '/storage/' . str_replace('\\', '/', $savename);
            return ApiUtil::normal($filename);
        }catch (ValidateException $e){
            return ApiUtil::normal(null, $e->getMessage(), ApiUtil::CODE_FAIL);
        }
    }
}